package com.syh.jn.scm.service.impl.supplier.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.logistics.Logistics;
import com.syh.jn.scm.domain.supplier.entity.base.logistics.LogisticsAddress;
import com.syh.jn.scm.domain.supplier.entity.base.logistics.LogisticsTruck;
import com.syh.jn.scm.domain.supplier.event.LogisticsEvent;
import com.syh.jn.scm.domain.supplier.repository.base.LogisticsRepository;
import com.syh.jn.scm.dto.supplier.base.logistics.*;
import com.syh.jn.scm.dto.supplier.base.logistics.logisticsAddress.LogisticsAddressMto;
import com.syh.jn.scm.dto.supplier.base.logistics.logisticsAddress.LogisticsAddressSto;
import com.syh.jn.scm.dto.supplier.base.logistics.logisticsAddress.LogisticsAddressVo;
import com.syh.jn.scm.dto.supplier.base.logistics.logisticsTruck.LogisticsTruckMto;
import com.syh.jn.scm.dto.supplier.base.logistics.logisticsTruck.LogisticsTruckSto;
import com.syh.jn.scm.dto.supplier.base.logistics.logisticsTruck.LogisticsTruckVo;
import com.syh.jn.scm.service.api.supplier.base.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 物流接口实现
 */
@Service
@Transactional
public class LogisticsServiceImpl extends AbstractServiceImpl<LogisticsSto, LogisticsMto, LogisticsVo, Logistics, Long> implements LogisticsService {

  @Autowired
  private LogisticsRepository logisticsRepositoryImpl;

  @Override
  protected AggregateRepository<Logistics, Long> getAggregateRepository() {
    return logisticsRepositoryImpl;
  }

  /**
   * 增加物流信息
   * @param sto
   * @return
   */
  @Override
  public Long save(LogisticsSto sto) {
    Logistics logistics = new Logistics(sto);
    Long id = logisticsRepositoryImpl.add(logistics);
    Logistics logisticsMq = logisticsRepositoryImpl.getById(id);
    //发送事件
    this.publicEvent(LogisticsEvent.buildLogisticsAddEvent(new Object[]{logisticsMq}));
    return id;
  }

  /**
   * 修改物流信息
   * @param mto
   */
  @Override
  public void update(LogisticsMto mto) {
    Logistics logistics = logisticsRepositoryImpl.getById(mto.getId());
    logistics.modify(mto);
    logisticsRepositoryImpl.update(logistics);
  }

  /**
   * 删除物流信息
   * @param id
   */
  @Override
  public void delete(Long id) {
    Logistics logistics = logisticsRepositoryImpl.getById(id);
    logistics.destroy();
    logisticsRepositoryImpl.remove(logistics);
    this.publicEvent(LogisticsEvent.buildLogisticsDeletedEvent(new Object[]{logistics}));
  }

  /**
   * 启用
   * @param id
   */
  @Override
  public void enabled(Long id) {
    Logistics logistics = logisticsRepositoryImpl.getById(id);
    logistics.enabled();
    logisticsRepositoryImpl.update(logistics);
  }

  /**
   * 停用
   * @param id
   */
  @Override
  public void disabled(Long id) {
    Logistics logistics = logisticsRepositoryImpl.getById(id);
    logistics.disabled();
    logisticsRepositoryImpl.update(logistics);
  }

  /**
   * 批量增加地址
   * @param stoList
   */
  @Override
  public void batchSaveAddress(List<LogisticsAddressSto> stoList) {
    List<LogisticsAddress> addressesList = new ArrayList<>();
    stoList.stream().forEach((sto)->{
      LogisticsAddress address = new LogisticsAddress(sto);
      addressesList.add(address);
    });
    logisticsRepositoryImpl.batchSaveAddress(addressesList);
  }

  /**
   * 批量修改地址
   * @param mtoList
   */
  @Override
  public void batchModifyAddress(List<LogisticsAddressMto> mtoList) {
    List<LogisticsAddress> addressesList = new ArrayList<>();
    mtoList.stream().forEach((mto)->{
      LogisticsAddress address = logisticsRepositoryImpl.getByAddressId(mto.getId());
      address.modify(mto);
      addressesList.add(address);
    });
    logisticsRepositoryImpl.batchModifyAddress(addressesList);
  }

  /**
   * 根据地址id删除
   * @param addressId
   */
  @Override
  public void deleteAddress(Long addressId) {
    LogisticsAddress address = logisticsRepositoryImpl.getByAddressId(addressId);
    address.destroy();
    logisticsRepositoryImpl.deleteAddress(address);
  }

  /**
   * 批量添加服务车型
   * @param stoList
   */
  @Override
  public void batchSaveTruck(List<LogisticsTruckSto> stoList) {
    List<LogisticsTruck> truckList = new ArrayList<>();
    stoList.stream().forEach((sto)->{
      LogisticsTruck truck = new LogisticsTruck(sto);
      truckList.add(truck);
    });
    logisticsRepositoryImpl.batchSaveTruck(truckList);
  }

  /**
   * 批量修改服务车型
   * @param mtoList
   */
  @Override
  public void batchModifyTruck(List<LogisticsTruckMto> mtoList) {
    List<LogisticsTruck> truckList = new ArrayList<>();
    mtoList.stream().forEach((mto)->{
      LogisticsTruck truck = logisticsRepositoryImpl.getByTruckId(mto.getId());
      truck.modify(mto);
      truckList.add(truck);
    });
    logisticsRepositoryImpl.batchModifyTruck(truckList);
  }

  /**
   * 根据车型id删除
   * @param truckId
   */
  @Override
  public void deleteTruck(Long truckId) {
    LogisticsTruck truck = logisticsRepositoryImpl.getByTruckId(truckId);
    truck.destroy();
    logisticsRepositoryImpl.deleteTruck(truck);
  }

  /**
   * 根据物流id查地址列表
   * @param logisticsId
   * @return
   */
  @Override
  public List<LogisticsAddressVo> getAddressListById(Long logisticsId) {
    return logisticsRepositoryImpl.getAddressListById(logisticsId);
  }

  /**
   * 根据物流id查车型列表
   * @param logisticsId
   * @return
   */
  @Override
  public List<LogisticsTruckVo> getTruckListById(Long logisticsId) {
    return logisticsRepositoryImpl.getTruckListById(logisticsId);
  }

  /**
   * 根据车型id获取车型
   * @param truckId
   * @return
   */
  @Override
  public LogisticsTruckVo getByTruckId(Long truckId) {
    LogisticsTruck entity = logisticsRepositoryImpl.getByTruckId(truckId);
    LogisticsTruckVo vo = new LogisticsTruckVo();
    ExtBeanUtils.copyProperties(entity, vo);
    return vo;
  }

  /**
   * 根据地址id获取地址
   * @param addressId
   * @return
   */
  @Override
  public LogisticsAddressVo getByAddressId(Long addressId) {
    LogisticsAddress entity = logisticsRepositoryImpl.getByAddressId(addressId);
    LogisticsAddressVo vo = new LogisticsAddressVo();
    ExtBeanUtils.copyProperties(entity, vo);
    return vo;
  }
}
